load("//tools:defaults.bzl", "jasmine_node_test", "pkg_npm", "ts_library")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "schematics_assets",
    srcs = glob([
        "**/files/**/*",
        "**/*.json",
    ]) + [
        "README.md",
    ],
)

ts_library(
    name = "schematics",
    package_name = "@nguniversal/express-engine/schematics",
    srcs = glob(
        ["**/*.ts"],
        exclude = [
            "testing/**/*",
            "**/*.spec.ts",
            "**/files/**/*",
        ],
    ),
    tsconfig = ":tsconfig.json",
    deps = [
        "@npm//@angular-devkit/core",
        "@npm//@angular-devkit/schematics",
        "@npm//@schematics/angular",
        "@npm//rxjs",
        "@npm//typescript",
    ],
)

# This package is intended to be combined into the main @nguniversal/express-engine package as a dep.
pkg_npm(
    name = "npm_package",
    # We never set a `package_name` for NPM packages, neither do we enable validation.
    # This is necessary because the source targets of the NPM packages all have
    # package names set and setting a similar `package_name` on the NPM package would
    # result in duplicate linker mappings that will conflict. e.g. consider the following
    # scenario: We have a `ts_library` for `@angular/core`. We will configure a package
    # name for the target so that it can be resolved in NodeJS executions from `node_modules`.
    # If we'd also set a `package_name` for the associated `pkg_npm` target, there would be
    # two mappings for `@angular/core` and the linker will complain. For a better development
    # experience, we want the mapping to resolve to the direct outputs of the `ts_library`
    # instead of requiring tests and other targets to assemble the NPM package first.
    # TODO(alan-agius4): consider removing this if `rules_nodejs` allows for duplicate
    # linker mappings where transitive-determined mappings are skipped on conflicts.
    # https://github.com/bazelbuild/rules_nodejs/issues/2810.
    package_name = None,
    srcs = [":schematics_assets"],
    validate = False,
    deps = [":schematics"],
)

### Testing rules

jasmine_node_test(
    name = "unit_tests",
    srcs = [":schematics_test_sources"],
    data = [
        ":schematics_assets",
    ],
)

ts_library(
    name = "schematics_test_sources",
    testonly = True,
    srcs = glob(
        [
            "testing/*.ts",
            "**/*.spec.ts",
        ],
        exclude = ["**/files/**/*"],
    ),
    deps = [
        ":schematics",
        "@npm//@angular-devkit/core",
        "@npm//@angular-devkit/schematics",
        "@npm//@schematics/angular",
    ],
)
